package com.bp.hearthealth.pulse.lnapps.notification

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.ads.admob.open_ad.OnShowAdCompleteListener
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.SplashAdShowActivity
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.TopOnOpenAdManager
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.SplashType
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.TopOnSplashFinishObservable
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.TopOnSplashFinishObserver
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_blood_pressure.BpBloodPressureNewRecordActivity
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_heart.bp_guide.BpHeartRateGuideActivity
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_language_gender.bp_splash.BpSplashActivity
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_main.BpMainActivity
import com.bp.hearthealth.pulse.lnapps.bp_utils.BPActivityCollector
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants

class NotificationClickActivity : AppCompatActivity(), TopOnSplashFinishObserver {

    private var notificationAction:String? = ""

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        Log.i("NotificationClickActivity", "onNewIntent")
        if (intent != null) {
            init(intent)
        } else {
            Log.i("NotificationClickActivity", "onNewIntent intent == null")
            finish()
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("NotificationClickActivity", "通知  创建")
        init(intent)
    }

    private fun init(intent: Intent) {
        val notificationAction = intent.getStringExtra(Constants.NOTIFICATION_ACTION)
        LogUtil.i("notificationAction: $notificationAction")
        when (notificationAction) {
            Constants.NOTIFICATION_HOME_FRAGMENT -> {
                if (BPActivityCollector.isActivityDestroy(BpMainActivity::class.java)) {
                    startActivity(Intent(this, BpSplashActivity::class.java))
                } else {
//                    showBpOpenAd(notificationAction)
                    showTopOnOpenAd(notificationAction)
                }
            }

            Constants.NOTIFICATION_LIFESTYLE_FRAGMENT -> {
                if (BPActivityCollector.isActivityDestroy(BpMainActivity::class.java)) {
                    Constants.LIFE_STYLE_JUMP = true//首页LifeStyle页面
                    startActivity(Intent(this, BpSplashActivity::class.java))
                } else {
//                    showBpOpenAd(notificationAction)
                    showTopOnOpenAd(notificationAction)
                }
            }

            Constants.NOTIFICATION_HEART_RATE_GUIDE -> {
                if (BPActivityCollector.isActivityDestroy(BpMainActivity::class.java)) {
                    Constants.HEART_RATE_JUMP = true//心率引导
                    startActivity(Intent(this, BpSplashActivity::class.java))
                } else {
//                    showBpOpenAd(notificationAction)
                    showTopOnOpenAd(notificationAction)
                }
            }

            Constants.NOTIFICATION_BLOOD_PRESSURE -> {
                if (BPActivityCollector.isActivityDestroy(BpMainActivity::class.java)) {
                    Constants.BLOOD_PRESSURE_JUMP = true//血压检测
                    startActivity(Intent(this, BpSplashActivity::class.java))
                } else {
//                    showBpOpenAd(notificationAction)
                    showTopOnOpenAd(notificationAction)
                }
            }
        }
    }

    private fun showBpOpenAd(notificationAction: String?) {
        BpApplication.adMobOpenAdManager?.showAdIfAvailable(
            this,
            object : OnShowAdCompleteListener {
                override fun onShowAdComplete() {
                    startBpActivity(notificationAction)
                }

                override fun onAdShowedFullScreenContent() {

                }

                override fun onAdFailedToLoad() {
                    startBpActivity(notificationAction)
                }
            })

    }

    private fun showTopOnOpenAd(notificationAction: String?) {
        this.notificationAction = notificationAction
        TopOnSplashFinishObservable.registerSplash(this)
        if (TopOnOpenAdManager.isAdReady()) {
            startActivity(
                Intent(
                    this,
                    SplashAdShowActivity::class.java
                )
            )
        } else {
            startBpActivity(notificationAction)
        }
    }

    private fun startBpActivity(notificationAction: String?) {
        Log.i("NotificationClickActivity", "通知  startBpActivity")
        notificationAction?.let {
            when (it) {
                Constants.NOTIFICATION_HOME_FRAGMENT -> {
                    Log.i("NotificationClickActivity", "启动Home")
                    startActivity(
                        Intent(
                            this@NotificationClickActivity,
                            BpMainActivity::class.java
                        )
                    )
                }

                Constants.NOTIFICATION_LIFESTYLE_FRAGMENT -> {
                    Log.i("NotificationClickActivity", "启动LifeStyle")
                    val intent = Intent(
                        this@NotificationClickActivity,
                        BpMainActivity::class.java
                    )
                    intent.putExtra("notification", 2)
                    startActivity(intent)
                }

                Constants.NOTIFICATION_HEART_RATE_GUIDE -> {
                    Log.i("NotificationClickActivity", "启动心率")
                    startActivity(
                        Intent(
                            this@NotificationClickActivity,
                            BpHeartRateGuideActivity::class.java
                        )
                    )
                }

                Constants.NOTIFICATION_BLOOD_PRESSURE -> {
                    Log.i("NotificationClickActivity", "启动血压")
                    startActivity(
                        Intent(
                            this@NotificationClickActivity,
                            BpBloodPressureNewRecordActivity::class.java
                        )
                    )
                }
            }
        }
        finish()
    }

    override fun onDestroy() {
        TopOnSplashFinishObservable.unRegisterSplash(this)
//        overridePendingTransition(R.anim.fade_in, 0)
        super.onDestroy()
        Log.i("NotificationClickActivity", "销毁通知activity")
    }

    override fun topOnSplashFinish(splashType: SplashType) {
        LogUtil.iTopOn("splashType $splashType")
        startBpActivity(notificationAction)
    }
}